import {voteModel} from "../../../model/vote"
import {config} from '../../../config'
var vote = new voteModel()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    detail:{},
    newestVote:[],
    recommend:[],
    showActionsheet: false,
    groups: [
        { text: '发送给朋友', value: 1 },
        { text: '生成海报', value: 2 },
    ],
  },
  close: function () {
    this.setData({
        showActionsheet: false
    })
  },
  btnClick(e) {
    console.log(e)
    switch(e.detail.value){
      case 1:
        console.log(e.detail.value)
        break;
      case 2:
        this.selectComponent("#poster").formSubmit()
        break;
    }
    this.close()
  },
  shareAction:function(e){
    if(config.isMobile !="1"){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
    this.setData({
      showActionsheet: true
    })
  }
  },
  getDetail(){
    vote.detail(this.data.id).then(
      res=>{
        if(res.code === 0 ){
          console.log(res)
          this.setData({
            detail:res.data
          })
        }
      }
    )
  },
  //投票
  Vote(){
    if(config.isMobile !="1"){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
    vote.vote(this.data.id).then(
      res=>{
        if(res.code === 0){
          wx.showToast({
            title:'投票成功！',        
            icon:'success',         
            duration: 2000         
          })    
        }else{
          wx.showToast({
            title:res.msg,        
            icon:'none',         
            duration: 2000         
          }) 
        }
      }
    )
    }
  },
  //姐姐力荐
  getRecommend(){
    vote.recommend().then(
      res=>{
        if(res.code === 0 ){
          console.log(res)
          this.setData({
            recommend:res.data
          })
        }
      }
    )
  },
  getNewestVote(){
    vote.newestVote(7,1,JSON.stringify({voteId:this.data.id})).then(
      res=>{
        if(res.code === 0 ){
          console.log(res)
          this.setData({
            newestVote:res.data
          })
        }
      }
    )
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options){
      this.data.id = options.id
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetail()
    this.getNewestVote()
    this.getRecommend()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let url = encodeURIComponent('/pages/vote/detail/detail?id='+this.data.id);
    return {
      title: "test",
      path: `/pages/vote/list/list?url=${url}`
    }
  }
})